import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';
import { GenderType, GenderNameList, CleanerStatus, CleanerStatusNameList } from '../constants';
import * as moment from 'moment';

/**
 * 保洁师集合
 */
export default (app: Application) => {
  const mongoose = app.mongoose;
  const cleanerSchema = new mongoose.Schema({
    // 姓名
    name: {
      type: String,
      required: true,
    },
    // 性别
    gender: {
      type: Number,
      required: true,
      default: GenderType.female,
      enum: [ GenderType.female, GenderType.male ],
    },
    // 生日
    birthday: {
      type: Date,
      required: true,
    },
    // 头像
    avatarUrl: {
      type: String,
      required: true,
    },
    // 联系电话
    phoneNumber: {
      type: Number,
      required: true,
    },
    // 家庭住址
    address: {
      type: String,
      required: true,
    },
    // 紧急联系人姓名
    emergencyName: {
      type: String,
      required: true,
    },
    // 紧急联系电话
    emergencyPhoneNumber: {
      type: Number,
      required: true,
    },
    // 工作年限
    workAge: {
      type: String,
      required: true,
    },
    // 擅长技能
    skills: {
      type: String,
      required: true,
    },
    // 等级
    grade: {
      type: Number,
      required: false,
    },
    // 状态
    status: {
      type: Number,
      required: true,
      enum: [ CleanerStatus.signing, CleanerStatus.released, CleanerStatus.pullBlack ],
    },
  }, { timestamps: true, toJSON: { virtuals: true, getters: true } });

  // 年龄
  // tslint:disable-next-line:ter-prefer-arrow-callback
  cleanerSchema.virtual('age').get(function (this: any) {
    return moment(this.birthday, 'YYYY-MM-DD').fromNow().split(' ')[0];
  });

  // 性别
  // tslint:disable-next-line:ter-prefer-arrow-callback
  cleanerSchema.virtual('genderName').get(function (this: any) {
    return GenderNameList[this.gender];
  });

  // 状态描述
  // tslint:disable-next-line:ter-prefer-arrow-callback
  cleanerSchema.virtual('statusName').get(function (this: any) {
    return CleanerStatusNameList[this.status] || '--';
  });

  cleanerSchema.index({ name: 1 });
  cleanerSchema.plugin(pageFindPlugin, {});
  return mongoose.model('Cleaner', cleanerSchema);
};
